package com.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping
public class MessageController {
    @RequestMapping("message")
    public String[] message(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String collect = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(","));
        Jwt jwt = (Jwt) authentication.getPrincipal();
        URL issuer = jwt.getIssuer();
        String tokenValue = jwt.getTokenValue();
        return new String[]{String.valueOf(tokenValue+"||"+issuer.toString()),collect};
    }
}
